package com.hdb.dao;

import java.lang.reflect.Field;
import java.util.List;

/**
 * Created by aszer on 2016/11/12.
 */
public class DataFilter {

    public static Object filterT(Object bean, Class<?> type) {
        try {
            if (!type.isAnnotationPresent(NeedFilter.class)) {
                return bean;
            }
            Field[] fileds = type.getDeclaredFields();
            for (Field f : fileds) {
                //基于标签注入
                if (f.isAnnotationPresent(Hidden.class)) {
                    //访问private字段
                    f.setAccessible(true);
                    //注入NULL属性
                    f.set(bean, null);
                }
            }
            return bean;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static List filterT(List beanList, Class<?> type) {
        for (Object bean : beanList) {
            if (filterT(bean, type) == null) {
                return null;
            }
        }
        return beanList;
    }

    public static List filterT(List<Object[]> beanList, Class<?> type1, Class<?> type2) {
        for (Object[] bean : beanList) {
            if (filterT(bean[0], type1) == null || filterT(bean[1], type2) == null) {
                return null;
            }
        }
        return beanList;
    }
}
